#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QCoreApplication>

#include "errnum.h"
#include "MCUObj.h"

MCUObj::MCUObj(QString name)
    : u_name(name)
    , u_list(new QList<PinObj*>())
    , u_peripheralTypes(new QMap<QString,QStringList*>)
    , u_peripheralModel(new PeripheralModel())
    , u_pinAFList(new PinAFList())
{
    /* 初始化 */
    u_list->clear();
    u_peripheralTypes->clear();
    fromjson();

    /* 测试 */
    // if (!u_list->isEmpty()) {
    //     Q_FOREACH (PinObj* pinObj, *u_list) {
    //         qDebug() << "perList:" << "\n\f";
    //         for(auto it = peripheralTypes()->begin(); it != peripheralTypes()->end(); it++)
    //         {
    //             qDebug() << *it << "\n\f";
    //         }
    //         qDebug() << "pinname:" << pinObj->name() << "\n\f";
    //         qDebug() << "pindef:"  << "\n\f";
    //         for(auto it = pinObj->pinDef()->begin(); it != pinObj->pinDef()->end(); it++)
    //         {
    //             qDebug() << it.key() << it.value() << "\n\f";
    //         }
    //     }

    // }

}

MCUObj::~MCUObj()
{
    delete u_pinAFList;
    u_pinAFList = nullptr;
    delete u_peripheralModel;
    u_peripheralModel = nullptr;

    if (!u_list->isEmpty()) {
        // 遍历列表
        Q_FOREACH (PinObj* pinObj, *u_list) {
            // 删除 PinObj 对象
            delete pinObj;
        }
        // 清除列表
        u_list->clear();
    }
}

QString MCUObj::name()
{
    return u_name;
}

int MCUObj::setName(QString name)
{
    u_name = name;
    return NONE_ERR;
}

QMap<QString,QStringList*>* MCUObj::peripheralTypes()
{
    return u_peripheralTypes;
}

int MCUObj::addToPeripheralTypes(QString pname)
{
    //*u_peripheralTypes << pname;
    return NONE_ERR;
}

PeripheralModel* MCUObj::peripheralModel()
{
    qDebug() << u_peripheralModel;
    return u_peripheralModel;
}

int MCUObj::setPeripheralModel(PeripheralModel* model)
{
    u_peripheralModel = model;
    return NONE_ERR;
}

PinAFList* MCUObj::pinAFList()
{
    return u_pinAFList;
}

int MCUObj::fromjson()
{
    /* 打开文件 */
    //QString absolutePath = QCoreApplication::applicationDirPath() + "./iodate/" + u_name + ".txt";
    QString absolutePath = "../../iodate/" + u_name + ".txt";   //调试用的位置，发布时需要改位置
    QFile file(absolutePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << absolutePath;
        return false;
    }

    /* 保存引脚 */

    QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());
    qDebug() <<"fromJson(file.readAll());";
    if (jsonDoc.isNull()) {
        qDebug() << "Unable to parse the JSON." + u_name;
        return false;
    }
    QJsonArray jsonArray_all = jsonDoc.array();
    for(auto pin_it = jsonArray_all.begin(); pin_it != jsonArray_all.end(); pin_it++)
    {
        if(!(pin_it->isObject())) {qDebug() <<"piniodate_file error";return ERR_1;}
        const QJsonObject jsonObject_group = pin_it->toObject();

        PinObj* pin = new PinObj();

        QString pinname = jsonObject_group.value("PinName").toString();
        if(pinname != "")
        {
            pin->setName(pinname);
            const QJsonObject jsonObject_af = jsonObject_group.value("PinDefinition").toObject();
            for (auto it = jsonObject_af.begin(); it != jsonObject_af.end(); ++it) {
                pin->setPinDef(it.key(), it.value().toString());
            }
        }

        if (!jsonArray_all.isEmpty()) {
            // 获取第一个 JSON 对象
            QJsonObject firstJsonObject = jsonArray_all[0].toObject();  // 提取第一个对象

            // 遍历第一个 JSON 对象的键值对
            for (auto it = firstJsonObject.begin(); it != firstJsonObject.end(); ++it) {
                QString peripheralType = it.key();  // 外设类型名称（如 "ETH"）
                QJsonArray peripheralArray = it.value().toArray();  // 外设名称数组（如 ["ETH"]）

                // 创建一个 QStringList 来存储外设名称
                QStringList* peripheralList = new QStringList();

                // 将外设名称添加到 QStringList 中
                for (auto per_it = peripheralArray.begin(); per_it != peripheralArray.end(); ++per_it) {
                    peripheralList->append(per_it->toString());  // 添加外设名称
                }

                // 将外设类型和对应的外设名称列表添加到 QMap 中

                u_peripheralTypes->insert(peripheralType, peripheralList);
            }
        } else {
            qDebug() << "The JSON array is empty.";
            return false;
        }


        *u_list << pin;
    }

    return NONE_ERR;
}






